package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.FGIGantt;

public class FGIGanttPresenter extends AbstractPresenter<FGIGantt> {
	final String SINGLEITEM = "fgiGantt";
	final String MULTIITEM = "fGIGantts";
	
	public ArrayList<FGIGantt> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public FGIGantt tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public FGIGantt createItemFromJSONObject(JSONObject jsonObject){
		FGIGantt item = new FGIGantt();
		try{
			item.setFgiDesc(jsonObject.getString("fgiDesc"));
			item.setFgiQty(jsonObject.getInt("fgiQty"));
			item.setFgiRec(jsonObject.getInt("fgiRec"));
			item.setFgiVolRec(jsonObject.getDouble("fgiVolRec"));
			item.setFgiVolume(jsonObject.getDouble("fgiVolume"));
			item.setDaysTillNextBrew(jsonObject.getInt("daysTillNextBrew"));
			
			FGIViewPresenter presenter = new FGIViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("fgiID");
			item.setFgiID(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(FGIGantt item){
		try {
			JSONObject json = new JSONObject();
			json.put("fgiDesc", item.getFgiDesc());
			json.put("fgiQty", item.getFgiQty());
			json.put("fgiRec", item.getFgiRec());
			json.put("fgiVolRec", item.getFgiVolRec());
			json.put("fgiVolume", item.getFgiVolume());
			json.put("daysTillNextBrew", item.getDaysTillNextBrew());

			final FGIViewPresenter presenter = new FGIViewPresenter();
			json.put("fgiID", presenter.createJSON(item.getFgiID()));

			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public FGIGantt getEmptyEntity(){
		FGIGantt entity = new FGIGantt();
		return entity;
	}
}
